﻿namespace ContactsSync.Providers.Outlook
{
    using System;
    using System.Windows;
    using ContactsSync.Infrastructure;
    using ContactsSync.Providers.Outlook.Providers;
    using ContactsSync.Providers.Outlook.Services;
    using ContactsSync.Providers.Outlook.ViewModels;
    using Microsoft.Practices.Composite.Modularity;
    using Microsoft.Practices.Composite.Regions;
    using Microsoft.Practices.Unity;

    public class OutlookModule : IModule
    {
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;

        public OutlookModule(IUnityContainer container, IRegionManager regionManager)
        {
            this.container = container;
            this.regionManager = regionManager;
        }

        public void Initialize()
        {
            InitializeResourceDictionary();
            this.RegisterViewsAndServices();

            OutlookViewModel outlookViewModel = this.container.Resolve<OutlookViewModel>();

            this.regionManager.Regions[Regions.MainRegion].Add(outlookViewModel);
        }

        private static void InitializeResourceDictionary()
        {
            ResourceDictionary dictionary = new ResourceDictionary();
            dictionary.Source = new Uri("ContactsSync.Providers.Outlook;component/Resources/OutlookResourceDictionary.xaml", UriKind.Relative);

            Application.Current.Resources.MergedDictionaries.Add(dictionary);
        }

        private void RegisterViewsAndServices()
        {
            this.container.RegisterType<IOutlookContactsProvider, OutlookContactsProvider>();
            this.container.RegisterType<IImageDownloader, ImageDownloader>();
        }
    }
}